package com.qipay.qipay.impl;

import com.qipay.baas.model.Order;
import com.qipay.baas.model.QipayTradlog;
import com.qipay.mapper.QipayTradlogMapper;
import com.qipay.qipay.QipayTradlogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service

public class QipayTradLogServiceImpl implements QipayTradlogService {

    @Autowired
    QipayTradlogMapper qipayTradlogMapper;

    @Override
    public QipayTradlog tradLog(QipayTradlog qipayTradlog) {
        qipayTradlog.setId((long) qipayTradlogMapper.insert(qipayTradlog));
        return qipayTradlog;
    }

    @Override
    @Transactional
    public void setOrderSuccess(Order order) {
        QipayTradlog qipayTradlog = new QipayTradlog();
        qipayTradlog.setSpId(order.getSpId());
        qipayTradlog.setChannelType(order.getChannelType());
        qipayTradlog.setSpFeeRecharge(order.getSpFeeRecharge());

        if (order.getAgentFee() == null || order.getAgentFee() == 0)
            qipayTradlog.setQipayAmount(order.getFee());
        else
            qipayTradlog.setQipayAmount(order.getAgentFee());

        qipayTradlog.setSpAmount(order.getSpFee());
        qipayTradlog.setTradeNo(order.getTradeNo());
        qipayTradlog.setTotal(order.getAmount());
        tradLog(qipayTradlog);
    }

    @Override
    public List<QipayTradlog> list(String from, String to) {
        return qipayTradlogMapper.list(from, to);
    }
}
